#!/usr/bin/env python
# -*- coding: utf-8 -*-

## build_database.py

from math import inf
from config import app, db
from models.data_model import Person, Probe, Subject, Experiment, Acquisition

PEOPLE_NOTES = [
    {
        "name": "Fairy",
        "duty": "Student",
        "role": "Admin",
    },
    {
        "name": "Ruprecht",
        "duty": "Teacher",
        "role": "User",
    },
    {
        "name": "Bunny",
        "duty": "Teacher",
        "role": "User",
    },
]

PROBE_NOTES = [
    {
        "name": "L11-5V",
        "Nelements": 128,
        "fc": 7.6e6,
        "pitch": 300e-6,
        "kerf": 30e-6,
        "width": 270e-6,
        "bandwidth": 77,
        "radius": 100e3,
        "focus": 18e-3,
        "height": 5e-3,

    },
    {
        "name": "P4-2V",
        "Nelements": 64,
        "fc": 2.7e6,
        "pitch": 300e-6,
        "kerf": 60e-6,
        "width": 250e-6,
        "bandwidth": 74,
        "radius": 100e3,
        "focus": 60e-3,
        "height": 14e-3,
    },
]

SUBJECT_NOTES = [
    {
        "name": "牛长骨-1",
        "size": "3cm*3cm",
        "description": "made by YCS",
    },
    {
        "name": "牛长骨-2",
        "size": "4cm*3cm",
        "description": "made by hello",
    },
]

EXPERIMENTS_NOTES = [
    {
        "person_name": "Fairy",
        "subject_name": "牛长骨-1",
        "probe_name": "P4-2V",
        "remark": "first experiment for PAI",
        "exp_data_path": "D:/project/data/",
        "acquisitions": [
            {
                "acquisition_method":"single_angle_plane_wave",
                "sampling_frequency": 2.5,
                "laser_wavelength": 680,
                "laser_energy": 300,
                "trigger_mode": 3,
                "sampling_num": 4000,
                "tx2rx_delay": 400,
                "trigger_num": 10,
                "trigger_period": 100,
                "trigger_delay": 400,
                "line_number": 64,
                "pulse_width": 180,
                "pulse_pattern": "+-E",
                "pulse_repeat_num": 0,
                "acq_pa_LNA": 21,
                "acq_pa_PGA": 18,
                "acq_pa_Attenuation": 3,
                "acq_us_LNA": 17,
                "acq_us_PGA": 21,
                "acq_us_Attenuation": 21,
                "acq_voltage": 3000,
                "acq_data_path": "D:/project/data/123",
            },
            {
                "acquisition_method":"multi_angle_compound_plane_wave",
                "sampling_frequency": 2.5,
                "laser_wavelength": 680,
                "laser_energy": 300,
                "trigger_mode": 3,
                "sampling_num": 4000,
                "tx2rx_delay": 400,
                "trigger_num": 10,
                "trigger_period": 100,
                "trigger_delay": 400,
                "line_number": 64,
                "pulse_width": 180,
                "pulse_pattern": "+-E",
                "pulse_repeat_num": 0,
                "acq_pa_LNA": 21,
                "acq_pa_PGA": 18,
                "acq_pa_Attenuation": 3,
                "acq_us_LNA": 17,
                "acq_us_PGA": 21,
                "acq_us_Attenuation": 21,
                "acq_voltage": 3000,
                "acq_data_path": "D:/project/data/234",
            },
        ],
    },
    {
        "person_name": "Ruprecht",
        "subject_name": "牛长骨-2",
        "probe_name": "P4-2V",
        "remark": "second experiment for PAI",
        "exp_data_path": "D:/project/data/",
        "acquisitions": [
            {
                "acquisition_method":"single_shot_multiple_receive",
                "sampling_frequency": 2.5,
                "laser_wavelength": 680,
                "laser_energy": 300,
                "trigger_mode": 2,
                "sampling_num": 4000,
                "tx2rx_delay": 400,
                "trigger_num": 10,
                "trigger_period": 100,
                "trigger_delay": 400,
                "line_number": 64,
                "pulse_width": 180,
                "pulse_pattern": "+-E",
                "pulse_repeat_num": 0,
                "acq_pa_LNA": 21,
                "acq_pa_PGA": 18,
                "acq_pa_Attenuation": 3,
                "acq_us_LNA": 17,
                "acq_us_PGA": 21,
                "acq_us_Attenuation": 21,
                "acq_voltage": 3000,
                "acq_data_path": "D:/project/data/123",
            },
            {
                "acquisition_method":"single_angle_plane_wave",
                "sampling_frequency": 2.5,
                "laser_wavelength": 680,
                "laser_energy": 300,
                "trigger_mode": 2,
                "sampling_num": 4000,
                "tx2rx_delay": 400,
                "trigger_num": 10,
                "trigger_period": 100,
                "trigger_delay": 400,
                "line_number": 64,
                "pulse_width": 180,
                "pulse_pattern": "+-E",
                "pulse_repeat_num": 0,
                "acq_pa_LNA": 21,
                "acq_pa_PGA": 18,
                "acq_pa_Attenuation": 3,
                "acq_us_LNA": 17,
                "acq_us_PGA": 21,
                "acq_us_Attenuation": 21,
                "acq_voltage": 3000,
                "acq_data_path": "D:/project/data/234",
            },
        ],
    },
]


with app.app_context():
    db.drop_all()
    db.create_all()
    for people in PEOPLE_NOTES:
        new_person = Person(name=people.get("name"), duty=people.get("duty"), role=people.get("role"))
        db.session.add(new_person)

    for probe in PROBE_NOTES:
        new_probe = Probe(name=probe.get("name"), 
                          Nelements=probe.get("Nelements"), 
                          fc=probe.get("fc"), 
                          pitch=probe.get("pitch"), 
                          kerf=probe.get("kerf"),
                          width=probe.get("width"),
                          bandwidth=probe.get("bandwidth"),
                          radius=probe.get("radius"),
                          focus=probe.get("focus"),
                          height=probe.get("height"))
        db.session.add(new_probe)

    for subject in SUBJECT_NOTES:
        new_subject = Subject(name=subject.get("name"), size=subject.get("size"), description=subject.get("description"))
        db.session.add(new_subject)
    
    for experiment in EXPERIMENTS_NOTES:
        new_experiment = Experiment(person_name = experiment.get("person_name"),
                                    subject_name = experiment.get("subject_name"),
                                    probe_name = experiment.get("probe_name"),
                                    remark = experiment.get("remark"),
                                    exp_data_path = experiment.get("exp_data_path") )
        for acq in experiment.get("acquisitions"):
            new_experiment.acquisitions.append(
                Acquisition(
                    acquisition_method = acq.get("acquisition_method"),
                    sampling_frequency = acq.get("sampling_frequency"),
                    laser_wavelength = acq.get("laser_wavelength"),
                    laser_energy = acq.get("laser_energy"),
                    trigger_mode = acq.get("trigger_mode"),
                    sampling_num = acq.get("sampling_num"),
                    tx2rx_delay = acq.get("tx2rx_delay"),
                    trigger_num = acq.get("trigger_num"),
                    trigger_period = acq.get("trigger_period"),
                    trigger_delay = acq.get("trigger_delay"),
                    line_number = acq.get("line_number"),
                    pulse_width = acq.get("pulse_width"),
                    pulse_pattern = acq.get("pulse_pattern"),
                    pulse_repeat_num = acq.get("pulse_repeat_num"),
                    acq_pa_LNA = acq.get("acq_pa_LNA"),
                    acq_pa_PGA = acq.get("acq_pa_PGA"),
                    acq_pa_Attenuation = acq.get("acq_pa_Attenuation"),
                    acq_us_LNA= acq.get("acq_us_LNA"),
                    acq_us_PGA= acq.get("acq_us_PGA"),
                    acq_us_Attenuation = acq.get("acq_us_Attenuation"),
                    acq_voltage = acq.get("acq_voltage"),
                    acq_data_path = acq.get("acq_data_path")
                )
            )
        db.session.add(new_experiment)

    db.session.commit()